package com.learner.common.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.joda.time.DateTime;
import org.springframework.util.StringUtils;

import javax.crypto.spec.SecretKeySpec;
import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.DatatypeConverter;
import java.security.Key;
import java.util.Date;

/**
 * @Description: jwt的一个工具类
 * @Author: ice2020x
 * @Date: 2021/10/25
 */
public class JwtUtils {

    public static final String APP_SECRET = "ukc8BDbRigUDaY6pZFfWus2jZWLPHO";

    private static Key getKeyInstance() {
        SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
        byte[] bytes = DatatypeConverter.parseBase64Binary(APP_SECRET);
        return new SecretKeySpec(bytes, signatureAlgorithm.getJcaName());
    }

    public static String getJwtToken(JwtInfo jwtInfo, int expire) {
        String token = "";
        try {
            token = Jwts.builder()
                    .setHeaderParam("typ", "JWT")
                    .setHeaderParam("alg", "HS256")
                    //主题
                    .setSubject("learner-user")
                    //颁发时间
                    .setIssuedAt(new Date())
                    //过期时间
                    .setExpiration(DateTime.now().plusSeconds(expire).toDate())
                    //用户id
                    .claim("id", jwtInfo.getUserId())
                    //用户昵称
                    .claim("nickname", jwtInfo.getNickname())
                    //用户头像
                    .claim("avatar", jwtInfo.getAvatar())
                    // 用户名
                    .claim("username",jwtInfo.getUsername())
                    .signWith(SignatureAlgorithm.HS256, getKeyInstance())
                    .compact();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return token;
    }

    /**
     * 判断token是否存在与有效
     *
     * @param jwtToken token
     * @return 判断结果
     */
    public static boolean checkJwtToken(String jwtToken) {
        if (StringUtils.isEmpty(jwtToken)) {
            return false;
        }
        try {
            Jwts.parser().setSigningKey(getKeyInstance()).parseClaimsJws(jwtToken);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 判断token是否存在与有效
     *
     * @param request request
     * @return 判断结果
     */
    public static boolean checkJwtToken(HttpServletRequest request) {
        try {
            String jwtToken = request.getHeader("token");
            if (StringUtils.isEmpty(jwtToken)) {
                return false;
            }
            Jwts.parser().setSigningKey(getKeyInstance()).parseClaimsJws(jwtToken);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 根据token获取用户信息
     *
     * @param request
     * @return
     */
    public static JwtInfo getInfoByJwtToken(HttpServletRequest request) {
        String jwtToken = request.getHeader("Authorization");
        if (StringUtils.isEmpty(jwtToken)) {
            throw new RuntimeException("未获取到token信息");
        }
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(getKeyInstance()).parseClaimsJws(jwtToken);
        Claims claims = claimsJws.getBody();
        return new JwtInfo(claims.get("id").toString(), claims.get("username").toString(), claims.get("nickname").toString(), claims.get("avatar").toString());
    }


    public static String getCurrentUserId(HttpServletRequest request) {
        try {
            JwtInfo infoByJwtToken = JwtUtils.getInfoByJwtToken(request);
            return infoByJwtToken.getUserId();
        } catch (Exception e) {
            throw new RuntimeException("未获取到token信息");
        }
    }
}
